//
//  HACGradientWave.m
//  HStockCharts
//
//  Created by Hotacool on 2018/9/19.
//

#import "HACGradientWave.h"
#import "HACLineProvider.h"
#import "HACBasicComponents.h"
#import "HACPainter.h"

@interface HACGradientWave ()
@property (nonatomic, strong) NSMutableArray<HACPathPainter*> *linePainterList;
@property (nonatomic, strong) NSMutableArray<HACGradienPathPainter*> *gradientPainterList;
@end

@implementation HACGradientWave

- (instancetype)initWithDataProvider:(id<HACChartDataProvider>)provider {
    if (self = [super initWithDataProvider:provider]) {
        _linePainterList = [NSMutableArray array];
        _gradientPainterList = [NSMutableArray array];
    }
    return self;
}

- (HACPathPainter*)getPainterWithIndex:(NSInteger)index {
    @synchronized(self){
        if (self.linePainterList.count > index) {
            return [self.linePainterList[index] clearContents];
        } else {
            HACPathPainter *player = [HACPathPainter painterLayer];
            [self.linePainterList addObject:player];
            return player;
        }
    }
}

- (HACGradienPathPainter*)getGradientPainterWithIndex:(NSInteger)index {
    @synchronized(self){
        if (self.gradientPainterList.count > index) {
            return [self.gradientPainterList[index] clearContents];
        } else {
            HACGradienPathPainter *player = [HACGradienPathPainter painterLayer];
            [self.gradientPainterList addObject:player];
            return player;
        }
    }
}

- (void)clean {
    [self.linePainterList enumerateObjectsUsingBlock:^(HACPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj clearContents];
    }];
    [self.gradientPainterList enumerateObjectsUsingBlock:^(HACGradienPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj clearContents];
    }];
}

- (void)removeBrush {
    [self.linePainterList enumerateObjectsUsingBlock:^(HACPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
    [self.gradientPainterList enumerateObjectsUsingBlock:^(HACGradienPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
}

- (void)drawInLayer:(CALayer*)layer inRect:(CGRect)rect {
    [super drawInLayer:layer inRect:rect];
    NSArray *datas;
    if ([self.provider conformsToProtocol:@protocol(HACLineProvider)]) {
        id<HACLineProvider> provider = (id<HACLineProvider>)self.provider;
        datas = [provider lineDatasInRect:rect];
    }
    [self.linePainterList enumerateObjectsUsingBlock:^(HACPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [[obj clearContents] removeFromSuperlayer];
    }];
    [self.gradientPainterList enumerateObjectsUsingBlock:^(HACGradienPathPainter * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [[obj clearContents] removeFromSuperlayer];
    }];
    int idx = 0;
    __block int gidx = 0;
    for (HACLineComponents *lineItem in datas) {
        CGPoint *points = lineItem.linePoints;
        NSMutableArray *pointsArray = lineItem.pointArray;
        HACPathPainter *player = [self getPainterWithIndex:idx++];
        player.strokeColor = lineItem.lineColor.CGColor;
        player.lineWidth = lineItem.lineBolder;
        player.fillColor = nil;
        player.anchorPoint = CGPointMake(0, 0);
        player.zPosition = 101;
        player.lineDashPattern = lineItem.dashLengths?:nil;
        //分段
        __weak typeof(self) weakSelf = self;
        NSArray <NSArray*>*subPoints = [self _generateSubPointsFromArray:pointsArray];
        if (subPoints) {
            [subPoints enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [player generateSubPath:^UIBezierPath *{
                    __strong typeof(weakSelf) strongSelf = weakSelf;
                    return [strongSelf _generatePathFromPoints:obj isSmooth:lineItem.isCurve];
                }];
                
                //渐变填充
                if (lineItem.fillColor) {
                    //渐变
                    HACGradienPathPainter *gplayer = [self getGradientPainterWithIndex:gidx++];
                    player.zPosition = 101;
                    gplayer.opacity = 0.16;
                    [gplayer generatePath:^UIBezierPath *{
                        __strong typeof(weakSelf) strongSelf = weakSelf;
                        return [strongSelf _generatePathFromPoints:obj isSmooth:lineItem.isCurve fillColor:[UIColor redColor] InRect:rect];
                    }];
                    gplayer.paintRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
                    gplayer.colors = @[(id)(lineItem.isGradient?[UIColor whiteColor].CGColor:lineItem.fillColor.CGColor)
                                       , (id)lineItem.fillColor.CGColor ];
                    [gplayer paintInLayer:layer];
                }
            }];
            [player paintInLayer:layer];
        }
    }
}
@end
